package view;

import controller.SilenceUserController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UnbanUserView {
    private final SilenceUserController controller = new SilenceUserController();
    private JComboBox<String> comboBox;

    public void showUi() {
        JFrame jf = new JFrame("解除禁言用户");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(MainView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JLabel jl1 = new JLabel("选择需解除禁言的用户");
        jl1.setFont(new Font("黑体", Font.BOLD, 24));
        JButton bu1 = new JButton("确      认");
        JButton bu4 = new JButton("退      出");

        comboBox = new JComboBox<>(controller.getSilenceUser());

        jp.setLayout(null);
        jl1.setBounds(125, 1, 300, 50);
        comboBox.setBounds(150, 100, 150, 50);
        bu1.setBounds(150, 200, 150, 50);
        bu4.setBounds(150, 300, 150, 50);

        jp.add(jl1);
        jf.add(bu1);
        jf.add(bu4);
        jf.add(comboBox);
        jf.add(jp);

        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);

        bu1.addActionListener(e -> {
            Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem != null) {
                controller.deleteSilenceUser((String) selectedItem);
                JOptionPane.showMessageDialog(null, "解除禁言成功");
                comboBox.setModel(new DefaultComboBoxModel<>(controller.getSilenceUser())); // 刷新 JComboBox
            } else {
                JOptionPane.showMessageDialog(null, "没有被禁言的用户");
            }
        });

        bu4.addActionListener(e -> jf.dispose());
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
    }
}